<?php

/**
 * Contrôleur des magasins
 */
class releves_controller extends Component
{
    // Attributs
    const byPage = 10;
    const page_identifier = 'page-';
    public $_pageTotal;

    /**
     * Constructeur
     */
    public function __construct()
    {
    }

    /**
     * Données à envoyer à la vue par défaut
     */
    public function defaut()
    {
        if (isset($_GET['param'][0])) {
            if (preg_match("/".self::page_identifier."/i", $_GET['param'][0])) {
                $p = explode(self::page_identifier, $_GET['param'][0]);
                $this->getAll($p[1]);
            } else {
                $tmp = explode('-',$_GET['param'][0]);
                $this->getOne($tmp[0]);
            }
        } else {
            $this->getAll();
        }
    }

    /**
     * Données à envoyer à la vue par défaut
     */
    public function releveCat()
    {
        if(!isset($_GET['param'][0]))
            header('location: '.BASE_URL.'reports/');
        
        $tmp = explode('-',$_GET['param'][0]);
        $id = $tmp[0];
        $releves = Base::Load(CLASS_RELEVE)->findBy('categorie', (string)$id, true);
        $cat = Base::Load(CLASS_PROD_CAT)->get($id);

        $this->addBreadCrumb($cat['name']);

        foreach($releves as $i => $r) {
            $prixTmp[] = $r['prix'];
            $p['prix'] = $r['prix'];
            $p['produit'] = $r['product']->name;
            $price[] = $p;
        }

        $pMediane = Tools::mediane($prixTmp);

        $this->_view->assign('prix', $price);
        $this->_view->assign('categorie', $cat);
        $this->_view->assign('medianes', $pMediane);
        $this->_view->addBlock('content', 'releveCat.tpl');
    }

    /**
     * Génère un pdf
     */
    public function pdfCat()
    {
        $tmp = explode('-',$_GET['param'][0]);
        $id = $tmp[0];
        $releves = Base::Load(CLASS_RELEVE)->findBy('categorie', (string)$id, true);
        $cat = Base::Load(CLASS_PROD_CAT)->get($id);

        foreach($releves as $i => $r) {
            $price[] = $r['prix'];
        }

        $pMediane = Tools::mediane($price);


        //var_dump($releves);exit();


        $this->_view->assign('prix', $price);
        $this->_view->assign('categorie', $cat);
        $this->_view->assign('medianes', $pMediane);
        $this->_view->assign('releves', $releves);
        Base::Load(CLASS_PDF)->simplePDF('pdfCat.tpl', 'macrise.com-releve-'.$cat['name'].'.pdf');
    }

    /**
     * Génère le PDF
     * @return void
     */
    public function pdf(){
        $tmp = explode('-',$_GET['param'][0]);
        $id = $tmp[0];

        $data = Base::Load(CLASS_PRODUCT)->get($id, true);
        $this->_view->assign('data', $data);
        $releves = Base::Load(CLASS_RELEVE)->findBy('product', (string)$data->_id, true);
        if(is_array($releves)){
            $releves = array_reverse($releves);
            $price = array();
            foreach($releves as $r){
                $price[] = $r['prix'];
            }

            $pMediane = Tools::mediane($price);
            $this->_view->assign('medianes', $pMediane);
            $this->_view->assign('releves', $releves);
        }
        Base::Load(CLASS_PDF)->simplePDF('pdf.tpl', 'macrise.com-releve-'.$data->name.'.pdf');
    }

    /**
     * Sélection d'un relevé
     *
     * @param: unsigned $id L'id d'un relevé
     */
    private function getOne($id)
    {
        $data = Base::Load(CLASS_PRODUCT)->get($id, true);
        $this->addBreadCrumb($data->name);

        $this->_view->assign('data', $data);
        
        $this->_view->assign('user_connected', Base::Load(CLASS_USER)->isConnect());
        
        $coms = Base::Load(CLASS_COMMENTAIRE)->findBy('object_id', (string) $id, true);
        $this->_view->assign('coms', $coms);

        $pMediane = Base::Load(CLASS_RELEVE)->getMediane($data->_id);

        $this->_view->assign('medianes', $pMediane['mediane']);
        $this->_view->assign('prix', $pMediane['price']);
        $this->_view->assign('releves', $pMediane['releves']);

        if(is_array($pMediane['releves'])){
            $this->_view->assign('releves_reverse', array_reverse($pMediane['releves']));
        }

        $this->_view->addBlock('content', 'releve.tpl');
    }

    /**
     * Récupération de toutes les villes
     *
     * @param unsigned $page L'endroit du tableau à visualiser (pagination)
     */
    private function getAll($page = 1)
    {
        $dataTmp = Base::Load(CLASS_RELEVE)->getAll();
        $this->_pageTotal = ceil(count($dataTmp) / self::byPage) + 1;
        $start = self::byPage * $page - self::byPage;
        $pageStart = $page - 5;

        if ($pageStart < 1) {
            $pageStart = 1;
        }

        foreach ($dataTmp as $i => $d) {
            if ($i >= $start) {
                if ($i < $start + self::byPage) {
                    $data[$i] = array();
                    $data[$i] = Base::Load(CLASS_RELEVE)->get($i, true);
                    $data[$i]->magasin->ville = Base::Load(CLASS_LOC)->getVille($data[$i]->magasin->ville);
                } else {
                    break;
                }
            }
        }

        $this->_view->assign('pageTotal', $this->_pageTotal);
        $this->_view->assign('page', $page);
        $this->_view->assign('pageStart', $pageStart);
        $this->_view->assign('pageStop', $pageStart + self::byPage - 1);
        $this->_view->assign('data', $data);

        if (isset($_GET['ajax']) && $_GET['ajax']) {
            $this->_view->assign('ajax', true);
            Base::Load(CLASS_BASE)->setLayout('vide.tpl');
            $this->_view->addBlock('data', 'list.tpl');
        } else {
            $this->_view->addBlock('content', 'list.tpl');
        }
    }

    public function POST_Commentaire(array $data)
    {   
        if (!empty($data['message'])) {
            $data['user'] = $_SESSION['user']['uid'];
            Base::Load(CLASS_COMMENTAIRE)->save($data);
            header('Location: ' . HTTP_HOST_REQUEST);
        }
    }
}


/* -- Fin du fichier -- */